﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;
using Cofe.Core.Implements;
using Cofe.Data.Utils.ThirdParty;

namespace Cofe.Data.Filter
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
    public class ParentGuidEntryFilterExpression : 
        IEntryFilterExpression<ParentGuidEntryFilter>
    {
        public Expression<Func<CofeSystemInfo, bool>>
            GetQueryExpression(ParentGuidEntryFilter entryFilter, IOptionFilter[] options)
        {            
            Guid lookupId = new Guid(entryFilter.Value);

            Expression<Func<CofeSystemInfo, bool>> exp;

            if (lookupId == CofeConstants.RootGuid)
                exp = e => e.DatabaseEntry.Parent == null;
            else
            {
                var propExpr = CofeServices.PropertyDictionary.GetPropertyExpression<Guid>(CacheProperties.ParentId);
                exp = LambaUtils.EqualExpr<Guid>(propExpr, lookupId);
            }
                  
            if (entryFilter.Negative)
                return exp.Not();
            return exp;
        }
    }
}
